package com.hsh.maoduo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.maoduo.common.Result;
import com.hsh.maoduo.pojo.entity.User;
import com.hsh.maoduo.pojo.vo.UserVO;
import com.hsh.maoduo.service.UserService;
import com.hsh.maoduo.mapper.UserMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

/**
* @author USER
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-03-09 13:00:20
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    /**
     * 登录
     * @param userVO
     * @return
     */
    @Override
    public Result login(UserVO userVO) {
        String username = userVO.getUsername();
        String password = userVO.getPassword();
        String md5pwd = DigestUtils.md5Hex(password);
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, md5pwd);

        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
        } catch (AuthenticationException e) {
            return Result.fail(e.getMessage());
        }
        return Result.success("/index",userVO.getUsername());

    }

    /**
     * 登录
     *
     * @return
     */
    @Override
    public Result logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Result.success("login");
    }
}




